//index.js
var app = getApp()
var util = require('../../utils/util.js')
var host = app.globalData.host
var self = null;

function getAdv() {
  var url = host + '/xhc/adv/list'
  app.request({
    url: url,
    data: {
      type: 1
    },
    success: function(res) {
      self.setData({
        adv: res.data.info
      })
    }
  })

  var url = host + '/xhc/ticket/init/'
  app.request({
    url: url,
    data: {},
    success: function(res) {
      app.globalData.agreement = res.data.info.agreement
      app.globalData.notice = res.data.info.notice
      app.globalData.notification = res.data.info.notification
      app.globalData.images = res.data.info.images
      app.globalData.openTime = res.data.info.openTime
      app.globalData.closeTime = res.data.info.closeTime
      self.setData({
        notification: app.globalData.notification,
        openTime: app.globalData.openTime,
        closeTime: app.globalData.closeTime,
        businessTime: res.data.info.businessTime
      })
    },
    fail: function(msg) {
      console.warn(msg)
    }
  })
}

function login() {
  app.getUserInfo(function(userInfo) {
    //更新数据
    self.setData({
      userInfo: userInfo,
    })
  })
}

Page({
  data: {
    userInfo: null,
    date: null,
    adv: [],
    list: [],
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    notification: '',
    openTime: '',
    closeTime: '',
    businessTime:''
  },
  getStyle() {
    wx.setNavigationBarTitle({
      title: '出出帮火车奇妙世界'
    })
    self.setData({
      themeColor: app.globalData.themeColor,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  getList() {
    app.request({
      url: host + '/xhc/ticket/list',
      data: {
        date: self.data.date
      },
      success: function(res) {
        var temp = res.data.info
        var today = new Date()
        var thisMonment = today.getTime()
        for (var index in temp) {
          var dateFmt = temp[index].time.replace(new RegExp(/-/gm), "/");
          var date = new Date(dateFmt);
          // var monment = date.getTime()
          // if (thisMonment > monment) {
          //   temp[index].enable = false
          // } else {
          //   temp[index].enable = true
          // }
          var hours = date.getHours()
          var lastHours = hours + 1;
          if (hours <= 9) {
            hours = '0' + hours
          }
          if (lastHours <= 9) {
            lastHours = '0' + lastHours
          }
          var time = hours + ':00' + '-' + lastHours + ':00'
          temp[index].time = time
        }
        self.setData({
          list: temp
        })
      },
      fail: function(msg) {
        console.warn(msg)
      }
    })
  },
  dateChange(e) {
    this.setData({
      date: e.detail.value
    })
    this.getList()
  },
  onPrev() {
    var date = new Date(self.data.date)
    date.setDate(date.getDate() - 1)
    var dateValue = util.dateFormat(date)
    this.setData({
      date: dateValue
    })
    this.getList()
  },
  onNext() {
    var date = new Date(self.data.date)
    date.setDate(date.getDate() + 1)
    var dateValue = util.dateFormat(date)
    this.setData({
      date: dateValue
    })
    this.getList()
  },
  pay(e) {
    var openId = app.globalData.openId
    console.log('openId:' + openId)
    if (!openId) {
      wx.navigateTo({
        url: '/pages/auth/auth?ticketId=' + ticketId,
      })
      return
    }
    var ticketId = e.currentTarget.dataset.ticketid
    wx.navigateTo({
      url: '/pages/agreement/agreement?ticketId=' + ticketId,
    })
  },
  payGroup() {
    wx.navigateTo({
      url: '/pages/groupPayment/groupPayment',
    })
  },
  onPullDownRefresh: function() {},
  content(e) {
    var images = e.currentTarget.dataset.images;
    var title = e.currentTarget.dataset.title;
    var url = e.currentTarget.dataset.url;
    if (url) {
      wx.navigateTo({
        url: '/pages/web/web?url=' + url
      })
    } else if (images) {
      wx.navigateTo({
        url: '/pages/content/content?images=' + images + '&title=' + title,
      })
    }
  },
  plusZhenyi() {
    wx.navigateTo({
      url: '/pages/zhenyi/zhenyi',
    })
  },
  //事件处理函数
  onShow: function() {
    if (this.userInfo == null) {
      login()
    }
    // this.marquee()
  },
  onLoad: function(options) {
    self = this
    self.getStyle()
    var today = util.dateFormat(new Date())
    self.setData({
      date: today,
      notification: app.globalData.notification,
      openTime: '08:00', //app.globalData.openTime,
      closeTime: '20:30', //app.globalData.closeTime
    })
    self.getList()
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo) {
      //更新数据
      self.setData({
        userInfo: userInfo,
        notification: app.globalData.notification,
        openTime: app.globalData.openTime,
        closeTime: app.globalData.closeTime
      })
    })
    getAdv()
  }
})